function noundef(obj) {
    if (obj == undefined) {
        return '';
    } else {
        return obj;
    }
}

function sidebar_nav_clone() {
    var uls = $('.sidebar-nav > ul > *').clone();
    uls.addClass('visible-xs');
    $('#jxd-menu').append(uls.clone());
}

function item_name_show(id) {
    var re = /([^_])_item:([^:]+):([^:]+):([^:]+):(.*)/g;
    var item_names = re.exec(id);
    return item_names[4] + '_' + item_names[5];
}

function id_char_translate(id) {
    var ret = id;
    ret = ret.replace(/:/g, "\\:");
    ret = ret.replace(/\./g, "\\.");
    ret = ret.replace(/\//g, "\\/");
    ret = ret.replace(/\$/g, "\\$");
    ret = ret.replace(/\[/g, "\\[");
    ret = ret.replace(/\]/g, "\\]");
    return ret;
}

function flot_id_translate(id, suffix) {
    return "#" + id_char_translate(id) + suffix;
}

function id_to_url(id, prefix) {
    var ret_url = prefix;
    var re = /item:([^:]+):([^:]+):([^:]+):(.*)/g;
    var id_url = re.exec(id+'/');
    ret_url = ret_url+id_url[1]+'/'+id_url[2]+'/'+id_url[3]+'/'+encodeURIComponent(id_url[4]);
    //console.log(ret_url);
    return ret_url;
}

function server_id_to_url(id, prefix) {
    var ret_url = prefix;
    var re = /server_item:([^:]+):([^:]+):([^:]+):(.*)/g;  /* server_item_北京_和盛大厦_debian_diskload_/ */
    var id_url = re.exec(id+'/');
    ret_url = ret_url+id_url[1]+'/'+id_url[2]+'/'+id_url[3]+'/'+encodeURIComponent(id_url[4]);
    //console.log(ret_url);
    return ret_url;
}

function pie_label_formatter(label, series) {
    return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
}

function port_update_table(id, msg) {
    //console.log("port_update_table", id, msg);
    var re = /([^_]+)_([^_]+)_(\d+)/g;
    $.each(msg, function(i, n) {
        var port_field = re.exec(n.name);
        //console.log(port_field);
        $(id).append('<tr><td>'+port_field[2]+'</td><td>'+port_field[3]+'</td><td>'+n.data[0][1]+'</td></tr>');
    });
}

function file_update_table(id, msg) {
    //console.log("file_update_table", id, msg);
    var re = /([^_]+)_(.+)/g;
    $.each(msg, function(i, n) {
        var file_field = re.exec(n.name);
        //console.log(port_field);
        $(id).append('<tr><td>'+file_field[2]+'</td><td>'+n.data[0][1]+'</td></tr>');
    });
}

function spline_show(id, data) {
    $(id).highcharts({
        chart: { type: 'spline' },
        title: { text: null },
        xAxis: { type: 'datetime',
                 dateTimeLabelFormats: { month: '%e. %b', year: '%b' },
                 title: { text: null }, },
        yAxis: { title: { text: null }, },
                 tooltip: { pointFormat: '{series.name}: {point.x:%e.%b}: <b>{point.y:.2f}</b>' },
        exporting: { enabled: false },
        legend: { enabled: false },
        credits: { enabled: false },
        plotOptions: {
            spline: {
                marker: { enabled: false },
                animation: false,
            }
        },
        series: data,
    });
}

function pie_show(id, pie_title, data) {
    $(id).highcharts({
        chart: { type: 'pie' },
        title: { text: pie_title },
        //tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' },
        exporting: { enabled: false },
        legend: { enabled: false },
        credits: { enabled: false },
        plotOptions: {
            pie: {
                dataLabels: {
                    enabled: true,
                    distance: -50,
                    style: { fontWeight: 'bold', color: 'white', textShadow: '0px 1px 2px black' }
                },
                animation: false,
            }
        },
        series: data,
        /* [{
            type: 'pie',
            name: 'Browser share',
            innerSize: '50%',
            data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                ['Chrome',   12.8],
            ]
        }] */
    });
}

function column_show(id, data) {
    $(id).highcharts({
        chart: { type: 'column' },
        title: { text: null },
        xAxis: { type: 'datetime',
                 dateTimeLabelFormats: { month: '%e. %b', year: '%b' },
                 title: { text: null }, },
        yAxis: { title: { text: null }, },
        tooltip: { pointFormat: '{series.name}: {point.x:%e.%b}: <b>{point.y:.2f}</b>' },
        exporting: { enabled: false },
        legend: { enabled: false },
        credits: { enabled: false },
        plotOptions: {
            column: {
                animation: false,
                threshold: 0,
                negativeColor: '#ff8800'
            }
        },
        series: data,
    });
}

function column_percent_show(id, categories_list, data) {
    $(id).highcharts({
        chart: { type: 'column' },
        title: { text: 'Stacked column chart'},
        xAxis: { labels: {rotation: -45},categories: categories_list },
        yAxis: { min: 0, title: { text: 'Total fruit consumption' } },
        tooltip: {
            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
            shared: true
        },
        plotOptions: {column: {stacking: 'percent'} },
        // [{name: 'John', data: [5, 3, 4, 7, 2] }, {name: 'Jane', data: [2, 2, 3, 2, 1] }, {name: 'Joe', data: [3, 4, 4, 2, 5] }]
        series: data
    });
}


// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function sec2time(sec, format) {
    var d1 = new Date(sec);
    var d2 = new Date();
    if (!sec || d1.getYear() == 70) {
        document.write('NaN');
        return;
    }
    if (format=='auto') {
        if (((d2-d1)/(1000*60*60)) < 24) {
            document.write(d1.Format('hh:mm'));
        } else {
            document.write(d1.Format('yyyy/M/d'));
        }
    } else {
        document.write(d1.Format(format));
    }
}

function email_list_format(email_list) {
    email_array = email_list.split(';')
    for (i in email_array) {
        document.write(email_array[i]+"<br/>");
    }
}

function fixup_format(fixup) {
    if (fixup == 0) {
        document.write('未处理');
    } else {
        document.write('已处理');
    }
}

function fixup_noval(per) {
    if (typeof(per) == "undefined") {
        return 0;
    } else {
        return per;
    }
}
function fixup_onoff(onoff) {
    if (typeof(onoff) == "undefined" || onoff == -1 || onoff == 'False' || onoff == false) {
        return('<span class="badge badge-danger"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></span>');
    } else {
        return('<span class="badge badge-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></span>');
    }
}
function sec2date(sec) {
    if (typeof(sec) == "undefined") {
        return('NaN');
    }
    var d1 = new Date(sec);
    var d2 = new Date();
    if (d1.getYear() == 70) {
        return('NaN');
    }
    return(d1.Format('yyyy-MM-dd'));
}
function fixup_attend_percent(attend, total) {
    if (typeof(attend) == "undefined" || typeof(total) == "undefined") {
        return "0/0";
    } else {
        perc = (attend/total)*100;
        ret = "<span>"+attend+" / "+total+"</span><br>";
        ret += "<span>"+perc.toFixed(2)+"%</span>";
        return ret;
    }
}
function fixup_version(appver, ctmver) {
    if (typeof(appver) == "undefined" || typeof(total) == "ctmver") {
        return "<span>V0.0</span><br><span>V0.0</span>";
    } else {
        return "<span>V"+appver+"</span><br><span>V"+ctmver+"</span>";
    }
}
function fixup_crstatus(cr_list) {
    if (typeof(cr_list) == "undefined") {
        return('NaN');
    }
    var cr_status="";
    for (var cr in cr_list) {
        //console.log(cr_list[cr]);
        if (cr_list[cr].status==false) {
            cr_status += '<span class="badge badge-danger">'+cr_list[cr].ip+'</span><br>';
        } else {
            cr_status += '<span class="badge badge-success">'+cr_list[cr].ip+'</span><br>';
        }
    }
    return cr_status;
}
function fixup_master_slaver(master_slaver, ip_100) {
    if (typeof(master_slaver) == "undefined" || typeof(ip_100) == "undefined" || master_slaver == false) {
        if (ip_100 == true) {
            return('<span class="badge badge-warning">M</span>');
        } else {
            return('<span class="badge badge-danger">F</span>');
        }
    }
    if (ip_100 == true) {
        return('<span class="badge badge-success">M</span>');
    } else {
        return('<span class="badge badge-success">S</span>');
    }
}

function mobile_number_check(tel) {
    var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
    if (reg.test(tel)) {
      return true;
    } else {
      return false;
    };
}
